$(function () {
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    };
    if (currentHotel === undefined || currentHotel === '') {
        toastr.error(lang('toastr.grogshop'));
        return;
    }     
    var couponType = ['',lang('Coupon.01'),lang('Coupon.02')];
    var requestCoupon = function () {
        toggleLoading(true);
        ajaxRequest('/Home/Coupon/getUserCoupon', {
            "hotel": currentHotel,
            "type": 2
        }, 'post', function (data) {
            toggleLoading(false);
            var info = data.data;
            var couponHTML = '';
            if (info.length === 0) {
                couponHTML = $('#templateCouponEmpty').clone().html();
            } else {
                $.each(info, function (key, value) {
                    var templateCouponHTML = $($('#templateCouponList').clone().html());
                    templateCouponHTML.data('couponid', value.couponId);
                    templateCouponHTML.find('.coupon-data').text(value.couponStart+'-'+value.couponEnd);
                    templateCouponHTML.find('.coupon-type').text(couponType[value.couponType]);
                    templateCouponHTML.find('.record-state').text(value.userCouponType);
                    templateCouponHTML.find('.coupon-name .info').text(value.couponName);
                    templateCouponHTML.find('.coupon-desc').text(lang('Coupon.03')+value.couponPrice+lang('Coupon.04'));
                    if(value.couponType == 1){
                        templateCouponHTML.find('.coupon-price').removeClass('hidden').find('span').text(value.couponMinusPrice);
                    }else{
                        templateCouponHTML.find('.coupon-discount').removeClass('hidden').find('span').text(value.couponDiscount);
                    }
                    couponHTML += templateCouponHTML.prop('outerHTML');
                });
            }
            $('#coupon-history').html(couponHTML);
        }, function(error){
            toggleLoading(false); 
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        });        
    }
    requestCoupon();
    $('#doc').on('click', '.coupon-refresh', function (e) {
        e.preventDefault();
        requestCoupon();
    });
});